package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 进行数据汇总
 */
@Service
public class ItemServiceImpl implements ItemService {


    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;

    /**
     * 获取skuinfo信息
     *
     * @param skuId
     * @return
     */
    @Override
    public Map<String, Object> getBySkuId(Long skuId) {
        Map<String, Object> result = new HashMap<>();


//        使用异步编排方式优化代码  supplyAsync有返回值
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //      获取商品详情信息，skuInfo信息
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            // 保存skuInfo
            result.put("skuInfo", skuInfo);
            return skuInfo;
        });

//        消费数据，无返回值
        CompletableFuture<Void> spuSaleAttrListCheckBySkuCompletableFuture = skuInfoCompletableFuture.thenAccept(skuInfo -> {
//            获取商品属性和属性值信息
            List<SpuSaleAttr> spuSaleAttrListCheckBySku =
                    productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            // 保存数据
            result.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
        });


        CompletableFuture<Void> SkuValueIdsMapCompletableFuture = skuInfoCompletableFuture.thenAccept(skuInfo -> {
            //            根据属性值id,确定不同属性规格的选择，即商品规格动态选择
            Map clientSkuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
//            转化为json数据给页面
            String JsonValue = JSON.toJSONString(clientSkuValueIdsMap);
            // 保存valuesSkuJson
            result.put("valuesSkuJson", JsonValue);
        });


        CompletableFuture<Void> skuPriceCompletableFuture = CompletableFuture.runAsync(() -> {
//            获取商品信息价格
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            // 获取价格
            result.put("price", skuPrice);
        });


        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
//            获取商品三级信息
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());

            //保存商品分类数据
            result.put("categoryView", categoryView);
        });

//        获取热度排名
        CompletableFuture<Void> hostScorecompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        });

//        使用多任务进行组合
        CompletableFuture.allOf(skuInfoCompletableFuture,
                spuSaleAttrListCheckBySkuCompletableFuture,
                SkuValueIdsMapCompletableFuture,
                skuPriceCompletableFuture,
                categoryViewCompletableFuture,
                hostScorecompletableFuture).join();
        return result;
    }
}
